from django.shortcuts import render, redirect
from utils.tencent.cos import delete_bucket
from web import models


def setting(request, project_id):
    return render(request, 'setting.html')


def project_delete(request, project_id):
    if request.method == 'GET':
        return render(request, 'project_delete.html')

    # 一、获取用户提交的想要删除的项目名称；
    project_name = request.POST.get('project_name')
    if not project_name or project_name != request.tracer.project.name:
        return render(request, 'project_delete.html', {'error': '项目不存在！'})

    # 二、判断是否为项目的创建者，只有项目的创建者才可以删除所创建项目；
    if request.tracer.user != request.tracer.project.creator:
        return render(request, 'project_delete.html',
                      {'error': f'只有本项目的创建者{request.tracer.project.creator}才有权限删除！'})

    # 三、删除桶；
            # 1、删除桶中所有的文件；
            # 2、删除桶中的所有碎片；
            # 3、删除桶；

    delete_bucket(request.tracer.project.bucket, request.tracer.project.region)

    # 四、在数据库中删除项目

    models.Project.objects.filter(id=request.tracer.project.id).delete()

    return redirect("project_list")
